import { createStore } from 'vuex';
import _ from 'lodash';
export default createStore({
   state: {
     user: {},
     token: "",
   },
   getters: {
     user(state) {
       var user = {};
       var localUser = JSON.parse(localStorage.getItem("user"));
       if(localUser) {
         user = localUser;
       }
       localStorage.setItem("user", JSON.stringify(user));
       state.user = user;
       return user;
     },
     token(state) {
       return state.token;
     }
   },
   mutations: {
     setUser(state, user) {
       var _user = _.merge({}, state.user);
       if (!_.size(_user)) {
         _user = {};
       }
       _.each(user, function (value, key) {
         _user[key] = value;
       })
       sessionStorage.setItem("user", JSON.stringify(_user));
       state.user = _user;
     },
     setToken(state, token) {
       localStorage.setItem("token", token)
       state.token = token;
     },
     logout(state) {
       sessionStorage.clear();
       state.user = {};
       state.token = null;
     }
   },
   actions: {
   },
   modules: {
   }
 })
